package org.primefaces.examples.moviecollector.dao;


import static org.junit.Assert.*;

import java.util.List;


import org.junit.Test;
import org.junit.runner.RunWith;
import org.primefaces.examples.moviecollector.domain.Eventos;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;


@RunWith(SpringJUnit4ClassRunner.class)
@Transactional
@TransactionConfiguration(transactionManager = "txManager")
@ContextConfiguration(locations = { "/applicationContext.xml" })
public class EventosDAOWithJPATest {
	
	private EventoDAO eventDAO;
	
	@Autowired
	public void setRepository(EventoDAO eventDAO){
		this.eventDAO=eventDAO;
	}
	
	@Test
	public void guardarEvento(){
		Eventos evet = new Eventos();
		evet.setNombre("Black and  White");
		
		eventDAO.persist(evet);
		assertNotNull(evet.getId());
	}
	
	@Test
	public void buscarPorNombre(){
		Eventos evet = new Eventos();
		evet.setNombre("Carnaval");
		
		eventDAO.persist(evet);
		assertNotNull(evet.getId());
		
		List<Eventos> resultado = eventDAO.findByNombre("Carnaval");
		assertEquals(1, resultado.size());
		assertEquals("Carnaval", resultado.get(0).getNombre());
		
	}
	
	@Test
	public void eliminar(){
	
		Eventos evet = new Eventos();
		evet.setNombre("Festival del Humor");
		
		eventDAO.persist(evet);
		
		eventDAO.delete(evet);
		
		
		assertFalse((!evet.getNombre().equals("Festival del Humor")));
		
		
	}
}



